<?php
class Admin_ScheduleController extends Zend_Controller_Action{

	public function init(){
		parent::init();
	}

	protected function getMenuSubInfo($mid) {
		if (isset($mid)) {
			$menuMapper = new Front_Model_MenuMapper();
			$menu = new Front_Model_Menu();
			$has = $menuMapper->find($mid, $menu);
			if ($has) {
				return $menu->_menuname;
			}
		}
		return '';
	}

	protected function buildTableSchedules() {
		$tableHtml = "<table cellpadding = '0' cellspacing = '0' class = 'table-data' border='0'>
					      <thead>
						  <tr>
						    <th>From date</th>
						    <th>To date</th>
						    <th>Mon</th>
						    <th>Tue</th>
						    <th>Wed</th>
						    <th>Thu</th>
						    <th>Fri</th>
						    <th>Sat</th>
						    <th>Sun</th>
						    <th>Update</th>
						    <th>Delete</th>
						  </tr>
						  </thead>
					  ";
		$scheduleMapper = new Admin_Model_ScheduleMapper();
		$entries = $scheduleMapper->fetchAll();

		if (isset($entries)) {
			$url = new Zend_View_Helper_Url();

			foreach ($entries as $row) {
				$rowStr = '<tr>';
				// from date
				$fromdate = $row->_fromdate;
				if (!isset($fromdate)) {
					$fromdate = '';
				}
				$rowStr = $rowStr .'<td>'.$fromdate.'</td>';
				// to date
				$todate = $row->_todate;
				if (!isset($todate)) {
					$todate = '';
				}
				$rowStr = $rowStr .'<td>'.$todate.'</td>';
				// mon
				$mon = $row->_mon;
				$mon = $this->getMenuSubInfo($mon);
				
				$rowStr = $rowStr .'<td>'.$mon.'</td>';
				// tue
				$tue = $row->_tue;
				$tue = $this->getMenuSubInfo($tue);
				
				$rowStr = $rowStr .'<td>'.$tue.'</td>';
				// wed
				$wed = $row->_wed;
				$wed = $this->getMenuSubInfo($wed);
				
				$rowStr = $rowStr .'<td>'.$wed.'</td>';
				// thu
				$thu = $row->_thu;
				$thu = $this->getMenuSubInfo($thu);
				
				$rowStr = $rowStr .'<td>'.$thu.'</td>';
				// fri
				$fri = $row->_fri;
				$fri = $this->getMenuSubInfo($fri);
				
				$rowStr = $rowStr .'<td>'.$fri.'</td>';
				// sat
				$sat = $row->_sat;
				$sat = $this->getMenuSubInfo($sat);
				
				$rowStr = $rowStr .'<td>'.$sat.'</td>';
				// sun
				$sun = $row->_sun;
				$sun = $this->getMenuSubInfo($sun);
				
				$rowStr = $rowStr .'<td>'.$sun.'</td>';

				// update link
				$updatelink = $url->url(array('module'=>'admin', 'controller'=>'schedule','action'=>'updateschedule','id'=>$row->_sid));
				if (!isset($updatelink)) {
					$updatelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$updatelink."'>Update</a>".'</td>';
				// delete link
				$deletelink = $url->url(array('module'=>'admin', 'controller'=>'schedule','action'=>'deleteschedule','id'=>$row->_sid));
				if (!isset($deletelink)) {
					$deletelink = '';
				}
				$rowStr = $rowStr ."<td><a href = '".$deletelink."'>Delete</a>".'</td>';
				$rowStr = $rowStr.'</tr>';
				$tableHtml = $tableHtml.$rowStr;
			}
		}

		$tableHtml = $tableHtml .'</table>';

		$this->view->schedules = $tableHtml;

	}


	public function indexAction() {
		$this->_forward('addschedule','schedule','admin');

	}

	public function addscheduleAction() {
		$this->buildTableSchedules();
		$formSchedule  = new Admin_Form_Schedule('create');
		$formSchedule->setMethod('post');
		//
		$menuMapper = new Front_Model_MenuMapper();
		$menus = $menuMapper->fetchAll();
		$formSchedule->bindData($menus);
		//
		$this->view->form = $formSchedule;
		if ($this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if ($formSchedule->isValid($formData)) {
				$schedule = new Admin_Model_Schedule();
				$this->populateSchedule($this->getRequest(), $schedule);
				$reason = 'create schedule failed';
				if ($this->checkSheduleValid($schedule, $reason, 'create')) {
					$scheduleMapper = new Admin_Model_ScheduleMapper();
					$scheduleMapper->save($schedule);
					My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'schedule', 'index');
				} else {
					echo $reason;
				}

			}

		}
	}

	protected function populateSchedule(Zend_Controller_Request_Abstract $request, Admin_Model_Schedule $schedule) {
		if (!isset($schedule)) {
			$schedule = new Admin_Model_Schedule();
		}
		$params = $request->getParams();
		if (isset($params)){
			$schedule->_sid = $request->getParam('sid');
			$schedule->_fromdate = $request->getParam('fromdate');
			$schedule->_todate = $request->getParam('todate');
			$schedule->_mon = $request->getParam('mon');
			$schedule->_tue = $request->getParam('tue');
			$schedule->_wed = $request->getParam('wed');
			$schedule->_thu = $request->getParam('thu');
			$schedule->_fri = $request->getParam('fri');
			$schedule->_sat = $request->getParam('sat');
			$schedule->_sun = $request->getParam('sun');
		}
	}

	protected function checkSheduleValid(Admin_Model_Schedule $schedule, &$reason, $type) {
		$valid = false;
		if (isset($schedule) && isset($schedule->_fromdate) && isset($schedule->_todate)) {
			if (Zend_Date::isDate($schedule->_fromdate, DATE_FORMAT) && Zend_Date::isDate($schedule->_todate, DATE_FORMAT)) {
				$fromdate = new Zend_Date();
				$fromdate->setDate($schedule->_fromdate, DATE_FORMAT);

				$todate = new Zend_Date();
				$todate->setDate($schedule->_todate, DATE_FORMAT);

				$dayOfWeekFromDate = $fromdate->get(Zend_Date::WEEKDAY_DIGIT);
				$dayOfWeekToDate = $todate->get(Zend_Date::WEEKDAY_DIGIT);


				$valid = true;
				if ($dayOfWeekFromDate != 1) {
					$reason = 'From date must be monday';
					$valid = false;
				}
				if ($dayOfWeekToDate != 0) {
					$reason = 'To date must be sunday';
					$valid = false;
				}

				if ($fromdate->isLater($todate)) {
					$reason = 'From date and todate must first day and end day of a week';
					$valid = false;
				} else {
					$diffTs = $todate->getDate()->get(Zend_Date::TIMESTAMP)
					- $fromdate->getDate()->get(Zend_Date::TIMESTAMP);
					if ($diffTs > 691200) {
						$reason = 'From date and todate must first day and end day of a week';
						$valid = false;
					}
				}
				if ($type == 'create') {
					$sMapper = new Admin_Model_ScheduleMapper();
					if ($sMapper->checkDuplicatePeriod($schedule->_fromdate, $schedule->_todate)) {
						$reason = 'Duplicate fromdate and todate';
						$valid = false;
					}
				}
			} else {
				$reason = 'Date format wrong';
			}
		}
		return $valid;
	}

	public function updatescheduleAction() {
		$this->buildTableSchedules();
		$sid = $this->getRequest()->getParam('id');
		$scheduleMapper = new Admin_Model_ScheduleMapper();
		$scheduleInfo = new Admin_Model_Schedule();
		$hasSchedule = $scheduleMapper->find($sid, $scheduleInfo);
		if (isset($hasSchedule) && isset($scheduleInfo)) {
			// Create form
			$formSchedule  = new Admin_Form_Schedule('update');
			$formSchedule->setMethod('post');
			//
			$menuMapper = new Front_Model_MenuMapper();
			$menus = $menuMapper->fetchAll();
			$formSchedule->bindData($menus);
			//
			$formSchedule->populateData($scheduleInfo);

			// Bind data
			$this->view->form = $formSchedule;

			if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($formSchedule->isValid($formData)) {
					$this->populateSchedule($this->getRequest(), $scheduleInfo);
					$scheduleInfo->_sid = $sid;
					$reason = 'update schedule failed';
					if ($this->checkSheduleValid($scheduleInfo, $reason, 'update')) {
						$scheduleMapper = new Admin_Model_ScheduleMapper();
						$scheduleMapper->save($scheduleInfo);
						My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'schedule', 'index');
					} else {
						echo $reason;
					}
				}

			}
		}
	}

	public function deletescheduleAction() {
		$sid = $this->getRequest()->getParam('id');
		$scheduleMapper = new Admin_Model_ScheduleMapper();
		if (isset($sid)) {
			$scheduleMapper->delete($sid);
			My_Controller_Plugin_Acl::goPage($this->getResponse(), 'admin', 'schedule', 'index');
		}
	}
}